/**
 * 工具类
 * 测试私钥加密功能 des加密
 * const test = des.encrypt('asdfwqweqwrqas', 'hahaha');
 * console.log(test);
 * console.log(aes.decrypt(test, 'hahaha'));
 */
import CryptoJS from 'crypto-js';
import crypto from 'crypto';

function getmd5(key) {
  const md5 = crypto.createHash('md5');
  md5.update(key);
  const keyStr = md5.digest('hex');
  const resKey = keyStr.substr(8, 16);
  return resKey;
}

export default { // 加密
  encrypt(word, keyStr) {
    const resKey = getmd5(keyStr);
    const keyHex = CryptoJS.enc.Utf8.parse(resKey);
    const srcs = CryptoJS.enc.Utf8.parse(word);
    const encrypted = CryptoJS.AES.encrypt(srcs, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  },
  // 解密
  decrypt(word, keyStr) {
    const resKey = getmd5(keyStr);
    const keyHex = CryptoJS.enc.Utf8.parse(resKey);
    const decrypt = CryptoJS.AES.decrypt(word, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  }
}
